﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://wpfimageviewer.codeplex.com
using System;
using System.Collections.ObjectModel;
using BrainTechLLC.ImageOperators;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;
using BrainTechLLC;
using CPPClassLibrary;
using DirectoryViewModels;
using ImageAppDataModels;
using ImageAppViewModels;
using System.IO;
using System.Collections.Generic;
using System.Text;
using BrainTechLLC.ThreadSafeObjects;
using System.Drawing;
using System.Windows.Media;
using System.Reflection;
using BinaryNorthwest;
using BrainTechLLC.FilterExtensions;


namespace WpfImageDuplicates
{
    public partial class Window1
    {
        private void imageCollectionView_RequestDelete(object sender, ObjectEventArgsNonRef<FileImageViewModel, bool> e)
        {
            var vm = e.obj1;
            var singleDelete = e.obj2;
            DeleteViewModel(vm, singleDelete);
        }

        private void imageCollectionView_RequestDeleteMany(object sender, ObjectEventArgs<List<FileImageViewModel>> e)
        {
            var images = e.obj;
            var result = MessageBox.Show(string.Format("Are you sure you want to delete the {0} selected image(s)?", images.Count), "Are you sure?", MessageBoxButton.YesNoCancel);

            if (result == MessageBoxResult.Yes || result == MessageBoxResult.OK)
            {
                FileImageCollectionViewModels.ForEach(o=>o.Delete(images));
            }
        }

        private void imageCollectionView_RequestResize(object sender, ObjectEventArgsNonRef<FileImageViewModel, bool> e)
        {
            var vm = e.obj1;
            var singleDelete = e.obj2;
            ResizeViewModel(vm, singleDelete);
        }

        private void imageCollectionView_RequestResizeDelete(object sender, ObjectEventArgsNonRef<FileImageViewModel, bool> e)
        {
            var vm = e.obj1;
            var singleDelete = e.obj2;
            ResizeDeleteViewModel(vm, singleDelete);
        }

        private void imageCollectionView_RequestRotate(object sender, ObjectEventArgsNonRef<RotationParams, bool> e)
        {
            var rotationParams = e.obj1;
            RotateViewModel(rotationParams);
        }

        public void RotateViewModel(RotationParams p)
        {
            RotateImage(p, false);
        }

        public void ResizeViewModel(FileImageViewModel vm, bool singleDelete)
        {
            ResizeImage(vm, singleDelete);
        }

        public void ResizeDeleteViewModel(FileImageViewModel vm, bool singleDelete)
        {
            MessageBoxResult result = ConfirmDelete(vm, singleDelete, "Are you sure you want to resize and then delete ");
            if (result == MessageBoxResult.Yes || result == MessageBoxResult.OK)
            {
                ResizeDeleteImage(vm, singleDelete);
            }
        }

        public void DeleteViewModel(FileImageViewModel vm, bool singleDelete)
        {
            MessageBoxResult result = ConfirmDelete(vm, singleDelete, "Are you sure you want to delete ");
            if (result == MessageBoxResult.Yes || result == MessageBoxResult.OK)
            {
                DeleteImage(vm, singleDelete);
            }
        }

        private static MessageBoxResult ConfirmDelete(FileImageViewModel vm, bool singleDelete, string text)
        {
            MessageBoxResult result = MessageBoxResult.No;
            if (singleDelete)
            {
                if (Settings.Values.ConfirmDelete)
                {
                    result = MessageBox.Show(string.Format("{0} {1}?", text, vm.FileNameOnly), "Are you sure?", MessageBoxButton.YesNoCancel);
                }
                else
                {
                    result = MessageBoxResult.Yes;
                }
            }
            else
            {
                if (Settings.Values.ConfirmMultiDelete)
                {
                    result = MessageBox.Show(string.Format("{0} the selected image(s)?", text), "Are you sure?", MessageBoxButton.YesNoCancel);
                }
                else
                {
                    result = MessageBoxResult.Yes;
                }
            }
            return result;
        }

        private void RotateImage(RotationParams p, bool singleDeleteOnly)
        {
            GeneralCommands.RotateCommand.Execute(p._vm, p._clockwise);
        }

        private void DeleteImage(FileImageViewModel vm, bool singleDeleteOnly)
        {
            GeneralCommands.DeleteCommand.Execute(vm, singleDeleteOnly);
            Commands.ImageDeletedCommand.Execute(vm);

            if (ImageDeleted != null)
                ImageDeleted(this, EventArgs.Empty);
        }

        private void ResizeImage(FileImageViewModel vm, bool singleDeleteOnly)
        {
            if (vm != null)
            {
                GeneralCommands.ResizeCommand.Execute(vm, Settings.Values.ResizeWidth, Settings.Values.ResizeHeight);
            }
        }

        private void ResizeDeleteImage(FileImageViewModel vm, bool singleDeleteOnly)
        {
            if (vm != null)
            {
                GeneralCommands.ResizeDeleteCommand.Execute(vm, Settings.Values.ResizeWidth, Settings.Values.ResizeHeight);
            }
        }

        void FullViewerHandler_RequestDelete(object sender, ObjectEventArgsNonRef<FileImageViewModel, bool> e)
        {
            var vm = e.obj1;
            var singleDelete = e.obj2;
            if (_FullViewerHandler.Handler.ImageViewModel == e.obj1)
            {
                var result = ConfirmDelete(vm, e.obj2, "Are you sure you want to delete ");
                if (result == MessageBoxResult.OK || result == MessageBoxResult.Yes)
                {
                    _FullViewerHandler.Handler.DeleteImage();
                }
            }
        }

        void CompareMultiple_RequestRotate(object sender, ObjectEventArgsNonRef<RotationParams, bool> e)
        {
            var rotationParams = e.obj1;
            RotateViewModel(rotationParams);
        }

        void CompareMultiple_RequestResizeDelete(object sender, ObjectEventArgsNonRef<FileImageViewModel, bool> e)
        {
            var vm = e.obj1;
            var singleDelete = e.obj2;
            ResizeDeleteViewModel(vm, singleDelete);
        }

        void CompareMultiple_RequestResize(object sender, ObjectEventArgsNonRef<FileImageViewModel, bool> e)
        {
            var vm = e.obj1;
            var singleDelete = e.obj2;
            ResizeViewModel(vm, singleDelete);
        }

        void CompareMultiple_RequestDelete(object sender, ObjectEventArgsNonRef<FileImageViewModel, bool> e)
        {
            var vm = e.obj1;
            var singleDelete = e.obj2;
            DeleteViewModel(vm, singleDelete);
        }

    }
}
